﻿/**
 * 1、双击标题栏可以在最大化和恢复原位置之间切换，最大化后不可拖动改变大小（右下角处）且不可以移动窗口（标题栏处），未最大化时都可以。
 * 2、
 *
 *
 *
 *
 *
 *
 *
*/

#ifndef MYTITLEBAR_H
#define MYTITLEBAR_H

#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>

class MyTitleBar : public QDialog
{
    Q_OBJECT
public:
    MyTitleBar(QWidget *parent =nullptr);

protected:
    bool eventFilter(QObject *obj,QEvent *evt) override;    //重写事件过滤器。标题栏点击移动窗体，双击最大化

private slots:
    void winMin();
    void winMax();
    void winClose();

public:    
    void setMainWidget(QWidget *mainWidget);          //设置主窗体
    void initControl();                               //初始化标题栏
    void initForm();
    void setLogoAndTitle(const QString &imageFile,const QString &title);


private:
    QVBoxLayout *verticalLayout;//上方是自定义标题栏，下方是主窗体
    //QWidget *mainWidget;    //主窗体对象
    //QWidget *widget;
    //QWidget *widgetTitle;

    //标题栏logo、title。可被直接访问，方便设置
    QLabel *labIco;
    QLabel *labTitle;
    //标题栏右侧按钮：皮肤、最小化、最大化、关闭
    QPushButton *btnWin_Skin;
    QPushButton *btnWin_Min;
    QPushButton *btnWin_Max;
    QPushButton *btnWin_Close;
    bool isWinMaximized;    //是否最大化
    QRect winLocation;      //窗体位置

};

#include<QLabel>
#include<QToolButton>
#include<QFrame>
#include<QMutex>
//图形字体处理类
class IconHelper : public QObject
{
    Q_OBJECT

public:
    explicit IconHelper(QObject *parent = nullptr);
    static IconHelper *Instance()
    {
        static QMutex mutex;

        if (!self) {
            QMutexLocker locker(&mutex);

            if (!self) {
                self = new IconHelper;
            }
        }

        return self;
    }

    void setIcon(QLabel *lab, QChar c, quint32 size = 9);
    void setIcon(QAbstractButton *btn, QChar c, quint32 size = 9);
    QPixmap getPixmap(const QString &color, QChar c, quint32 size = 9,
                      quint32 pixWidth = 10, quint32 pixHeight = 10);

    //根据按钮获取该按钮对应的图标
    QPixmap getPixmap(QToolButton *btn, bool normal);

    //指定导航面板样式,不带图标
    static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3,
                         const QString &borderColor = "#029FEA",
                         const QString &normalBgColor = "#292F38",
                         const QString &darkBgColor = "#1D2025",
                         const QString &normalTextColor = "#54626F",
                         const QString &darkTextColor = "#FDFDFD");

    //指定导航面板样式,带图标和效果切换
    void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> pixChar,
                  quint32 iconSize = 9, quint32 iconWidth = 10, quint32 iconHeight = 10,
                  const QString &type = "left", int borderWidth = 3,
                  const QString &borderColor = "#029FEA",
                  const QString &normalBgColor = "#292F38",
                  const QString &darkBgColor = "#1D2025",
                  const QString &normalTextColor = "#54626F",
                  const QString &darkTextColor = "#FDFDFD");

    //指定导航按钮样式,带图标和效果切换
    void setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> pixChar,
                  quint32 iconSize = 9, quint32 iconWidth = 10, quint32 iconHeight = 10,
                  const QString &normalBgColor = "#2FC5A2",
                  const QString &darkBgColor = "#3EA7E9",
                  const QString &normalTextColor = "#EEEEEE",
                  const QString &darkTextColor = "#FFFFFF");

protected:
    bool eventFilter(QObject *watched, QEvent *event);

private:
    static IconHelper *self;    //对象自身
    QFont iconFont;             //图形字体
    QList<QToolButton *> btns;  //按钮队列
    QList<QPixmap> pixNormal;   //正常图片队列
    QList<QPixmap> pixDark;     //加深图片队列
};

#endif // MYTITLEBAR_H
